<template>
	<view class="unit-container">
		<search-box @reset="resetUnit" @search="searchUnit" @add="goAddUnit" placeholder="请输入单元号" addText="新增单元"></search-box>
		<unit-list v-if="unitList" @edit="goEditUnit" @delete="goDeleteUnit" :unit-list="unitList"></unit-list>
		<view v-else>
			<van-empty description="暂无楼栋" />
		</view>
		<uni-load-more v-if="unitList && unitList.length >= 15" :status="status" />
	</view>
</template>

<script>
	import UnitList from '../components/unitList.vue'
	import SearchBox from '../components/searchBox.vue'
	
	export default {
		components: {
			SearchBox,
			UnitList
		},
		data() {
			return {
				status: 'more',
				unitList: [],
				params: {
					网格id: null,
					检查点id: null,
					pagination: {
						current: 1,
						pageSize: 20
					},
					sorter: {
						field: "id",
						order: 0
					}
				},
			}
		},
		onLoad(options) {
			this.params.网格id = uni.getStorageSync('gridInfo').所属网格id
			this.params.检查点id = +options.id
		},
		onShow() {
			this.getUnitList()
		},
		onReachBottom() {
			console.log('触发上拉')
			if(this.status !== 'noMore' && this.unitList) {
				this.status = 'loading'
				this.getMoreUnitList()
			}
		},
		methods: {
			resetUnit() {
				this.params.单元号 = null
				this.getUnitList()
			},
			searchUnit(event) {
				this.params.单元号 = event
				this.getUnitList()
			},
			getUnitList() {
				this.params.pagination.current = 1
				this.status = 'more'
				this.$http.post('gridSystem/unit/list', this.params).then(res => {
					console.log(res)
					if (res.code === 10000) {
						if(res.data.list !== null) {
							this.$common.transformUnit(res.data.list)
						}
						this.unitList = res.data.list
						if(this.unitList && this.unitList.length === res.data.total){
							this.status = 'noMore'
						}
					}
				})
			},
			getMoreUnitList() {
				this.params.pagination.current++
				this.$http.post('gridSystem/unit/list', this.params).then(res => {
					console.log(res)
					if(res.code === 10000){
						if(res.data.list !== null) {
							this.$common.transformUnit(res.data.list)
						}
						this.unitList = this.unitList && this.unitList.concat(res.data.list)
						if(this.unitList.length === res.data.total){
							this.status = 'noMore'
						}
					}
				})
			},
			goAddUnit() {
				uni.navigateTo({
					url: '/fireGridCheck/addUnit/addUnit?id=' + this.params.检查点id
				})
			},
			goEditUnit(item) {
				uni.navigateTo({
					url: '/fireGridCheck/unitDetail/unitDetail?item=' + JSON.stringify(item) + '&id=' + this.params.id
				})
			},
			goDeleteUnit(event) {
				this.$common.showModal('确定要删除该单元吗?', this.deleteUnit, event)
			},
			deleteUnit(obj) {
				this.$http.post('gridSystem/unit/delete', {
					单元id: obj.id
				}).then(res => {
					if (res.code === 10000) {
						this.unitList.splice(obj.index, 1)
						uni.showToast({
							title: '删除成功'
						})
						// this.getUnitList()
					}else if(res.code === 50012) {
						uni.showToast({
							title: '删除失败,单元下存在楼层',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.van-empty {
	background-color: #fff;
}
.unit-container {
	background-color: #f5f5f5;
}
</style>
